package stud.mpgame.flash.test;
import flash.utils.IDataInput;
import stud.mpgame.network.Message;
import stud.mpgame.network.UserObject;
import stud.mpgame.snapshot.ISnapshot;
import stud.mpgame.snapshot.ICommand;
import stud.mpgame.utils.ICloneable;

/**
 * ...
 * @author pavel
 */

class TestSnapshot extends UserObject, implements ISnapshot, implements ICloneable {
	
	private var value:Int;
	private var time:Int;
	
	public function new(value:Int, time:Int) {
		super();
		this.value = value;
		this.time = time;
	}	
	
	public function getTime():Int {
		return time;
	}	
	
	public function getValue():Int {
		return value;
	}
	
	public function setValue(value:Int):Void {
		this.value = value;
	}
	
	public function setTime(time:Int):Void {
		this.time = time;
	}
	
	public function interpolate(deltaTime:Int, serverSnapshot:ISnapshot):ISnapshot {
		var differenceTime:Int = serverSnapshot.getTime() - time;
		var differenceValue:Int = cast(serverSnapshot, TestSnapshot).getValue() - value;
		var addition:Int = Std.int(deltaTime * differenceValue * 1. / differenceTime);
		if (Math.abs(differenceValue) == 1)
			addition = differenceValue;
		var result: TestSnapshot = new TestSnapshot(value + addition, time + deltaTime);
		return result;
	}
	
	public function extrapolate(deltaTime:Int):ISnapshot {
		var result:TestSnapshot = new TestSnapshot(value + deltaTime, time + deltaTime);
		return result;
	}
	
	public function synchronize(serverSnapshot:ISnapshot):ISnapshot {
		return new TestSnapshot(value, time);
	}
	
    public override function readExternal(input:IDataInput, message:Message):Void {
        value = input.readInt();
		time = input.readInt();
    }
	
	public function getClone():ICloneable {
		var clone:TestSnapshot = new TestSnapshot(value, time);
		return clone;
	}
	
}